package org.linlinjava.litemall.db.util;

/*分页工具类
-----------------------------------------------*/

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <b> 分页通用类 </b>
 *
 * @param <T>
 * @author liftsail
 */
@Data
@Builder
public class PageTools<T> implements Serializable {
    /**
     * 实体类列表
     */
    private List<T> content;
    /**
     * 是否首页
     */
    private boolean first;
    /**
     * 是否尾页
     */
    private boolean last;
    /**
     * 总记录数
     */
    private Integer totalCount;
    /**
     * 总页数
     */
    private Integer totalPages;
    /**
     * 当前页记录数
     */
    private Integer count;
    /**
     * 每页记录数
     */
    private Integer pageSize;
    /**
     * 当前页
     */
    private Integer pageNum;


    //public List<T> pageUtil(Integer page, Integer size, List<T> list) {
    public void pageTools(Integer page, Integer size, List<T> list) {
        page = page <= 0 ? 1 : page;
        List<T> list1 = list.stream().skip((page - 1) * size).limit(size).collect(Collectors.toList());
        int length = list.size();
        //是否第一页
        this.first = (page == 1);
        //是否最后一页
        this.last = (page == (length - 1) / size);
        //总页数
        this.totalPages = ((length - 1) / size + 1);
        //总elements
        this.totalCount = (length);
        //每页多少elements
        this.pageSize = (size);
        //内容
        this.content = (list1);
        //当前页数据量
        this.count = (list1.size());
        //当前页数，第一页是1
        this.pageNum = (page);
        //return list1;
    }
}
